package com.pavco.caribbeanvisit.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.pavco.caribbeanvisit.client.RpcService;
import com.pavco.caribbeanvisit.client.RpcServiceAsync;
import com.pavco.caribbeanvisit.client.objects.LoginInfo;

public class Login extends Composite {

  private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class);

  interface LoginUiBinder extends UiBinder<Widget, Login> {
  }

  private LoginInfo loginInfo = null;

  @UiField
  Label gacLoginLabel;
  
  @UiField
  Anchor gacLoginAnchor;

  public Login() {    
    initWidget(uiBinder.createAndBindUi(this));
    
    RpcServiceAsync loginService = GWT.create(RpcService.class);
    loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
      @Override
      public void onFailure(Throwable error) {
      }

      @Override
      public void onSuccess(LoginInfo result) {
        loginInfo = result;
        if(loginInfo.isLoggedIn()) {
          gacLoginLabel.setText("Logged-in with your Google Account");
          gacLoginAnchor.setText("Sign Out");
          gacLoginAnchor.setHref(loginInfo.getLogoutUrl());
        } else {
          gacLoginLabel.setText("Login with your Google Account");
          gacLoginAnchor.setText("Sign In");
          gacLoginAnchor.setHref(loginInfo.getLoginUrl());
        }
      }
    });
  }  
}